package top.heliming.youyou.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.heliming.youyou.client.UserClient;
import top.heliming.youyou.entity.UserInfo;
import top.heliming.youyou.pojo.User;
import top.heliming.youyou.properties.JwtProperties;
import top.heliming.youyou.service.AuthService;
import top.heliming.youyou.util.JwtUtils;

/**
 * description: //TODO
 *
 * @author: heliming
 * @date:2019/06/09 下午 1:25
 */
@Service
public class AuthServiceImpl implements AuthService {


  @Autowired
  private UserClient userClient;

  @Autowired
  private JwtProperties properties;

  @Override
  public String authentication(String username, String password) {

    try {
      // 调用微服务，执行查询
      User user = this.userClient.queryUser(username, password);

      // 如果查询结果为null，则直接返回null
      if (user == null) {
        return null;
      }

      // 如果有查询结果，则生成token
      String token = JwtUtils.generateToken(new UserInfo(user.getId(), user.getUsername()),
          properties.getPrivateKey(), properties.getExpire());
      return token;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
}
